package ThreadTest;

import java.util.concurrent.*;

/**
 * @author tongchen
 * @create 2023-08-12 20:18
 */
public class CreateThread {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //继承thread类或者直接创建Thread对象
        MyThread myThread = new MyThread();
        myThread.start();
         Thread t1 =new Thread(()->{
             System.out.println("线程1执行任务....");
                 });
         t1.start();
         //实现runnable接口
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("线程2执行任务....");
            }
        };
        //创建Thread对象
        Thread thread = new Thread(runnable);
        thread.start();
        //通过callable和futureTask执行任务
        Callable callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int a=10;
                int b=20;
                return a+b;
            }
        };
        //创建futureTask
        FutureTask <Integer>futureTask = new FutureTask<>(callable);
       Thread t2=new Thread(futureTask);
       t2.start();
        Integer integer = futureTask.get();
        System.out.println("futureTask对应的结果为"+integer);
        //通过自定义线程池实现线程的创建
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 15, 10, TimeUnit.SECONDS, new LinkedBlockingQueue<>(10), new ThreadPoolExecutor.AbortPolicy());
        //给线程分配任务
        for(int i=0;i<200;++i){
            threadPoolExecutor.submit(()->{
                System.out.println(Thread.currentThread().getName()+"正在执行...");
            });
        }


    }

}
class MyThread extends Thread{
    @Override
    public void run() {
        System.out.println("线程执行任务.....");
    }
}
